/**
 * 使用 selenium - web 爬虫小说网站
 */

const {
  Builder,
  By,
  Key,
  until
} = require('selenium-webdriver');

let currentPageNum = 1;
let maxPageNum = 105;
var driver = new Builder().forBrowser('firefox').build();
var list = []
start()
async function start() {

  await driver.get('https://www.znlzd.com/bqg/113098/index_1.html#main'); //小说——我打造了长生俱乐部
  getData(driver)


};

async function getData() {
  console.log(`当前正在获取第${currentPageNum}页数据，共${maxPageNum}页`)
  while (true) {
    let notError = true;
    try {

      let items = await driver.findElements(By.css('.section-list li'));
      console.log(items)
      for (let i = 0; i < items.length; i++) {
        let item = items[i];
        // console.log(await  item.getText())
        let title = await item.findElement(By.css("a")).getText();
        let url = await item.findElement(By.css("a")).getAttribute("href");
        list.push({
          title,
          url
        });

      }
      console.log(list);
      console.log("------------currentPageNum-------------", currentPageNum)
      console.log("------------maxPageNum-------------", maxPageNum)
      if (currentPageNum < maxPageNum) {
        console.log("------------开始点击   下一页-------------")
        let pageItem = await driver.findElement(By.css('.listpage .right'));
        // console.log(pageItem.getText());
        pageItem.click();

        console.log("------------", getData)
        setTimeout(() => { //延迟获取数据，等待页面加载完成
          getData(driver);
        }, 2000)



      }
      console.log("list", list)

    } catch (error) {
      if (e) notError = false
      console.log("error ------------", error.message);

    } finally {
      if (notError) break;
    }
  }

}